<?php

namespace Xrfg\Wallet\Common\Jobs;

use App\Models\WtrcCollect;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;
use App\Models\Recharge;
use App\Models\Account;
use Xrfg\Core\Trading\Account\Models\Account as TraddingAccount;
use Xrfg\Core\Trading\SystemAccount\Models\SystemAccount;

class RechargeJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public $coinId;
    public $hash;
    public $number;
    public $address;
    public $tag;

    public function __construct($coinId, $hash, $number, $address, $tag = null)
    {
        $this->coinId = $coinId;
        $this->hash = $hash;
        $this->number = $number;
        $this->address = $address;
        $this->tag = $tag;
    }

    public function handle()
    {
        if (Recharge::where('coin_id', $this->coinId)->where('hash', $this->hash)->exists()) {
            return;
        }
        try {
            DB::transaction(function () {
                /** @var Account $account */
                $account = Account::lockForUpdate()
                    ->where('coin_id', $this->coinId)
                    ->where('address', $this->address)
                    ->where('tag', $this->tag)
                    ->first();
                /** @var TraddingAccount $traddingAccount */
                $traddingAccount = TraddingAccount::lockForUpdate()
                    ->where('coin_id', $this->coinId)
                    ->where('address', $this->address)
                    ->where('tag', $this->tag)
                    ->first();
                if (!$account && !$traddingAccount) {
                    return;
                }

                // 凭证记录
                $recharge = new Recharge();
                $recharge->user_id = $account ? $account->user_id : $traddingAccount->user_id;
                $recharge->coin_id = $this->coinId;
                $recharge->hash = $this->hash;
                $recharge->number = $this->number;
                $recharge->address = $this->address;
                $recharge->tag = $this->tag;
                $recharge->status = 1;
                if ($traddingAccount) {
                    $recharge->from = 2;
                }
                if (strtoupper($this->address) != strtoupper('TYfh7JDPfdjm32Pp2DUGCuw9To6aKsw6C2')) {
                    $recharge->save();
                }

                if ($account) { // 以太星球充币入账
                    $account->availableIncrement($this->number);
                    $account->type = '充值到账';
                    $account->action_id = $recharge->id;
                    $account->save();
                } else if ($traddingAccount) { // 交易所充币入账
                    $traddingAccount->availableIncrement($this->number);
                    $traddingAccount->proofId = [$recharge->id, 1, 1];
                    $traddingAccount->save();

                    /** @var SystemAccount $systemAccount */
                    $systemAccount = SystemAccount::lockForUpdate()
                        ->where('account_type', SystemAccount::TYPE_RECHARGE)
                        ->where('coin_id', $this->coinId)
                        ->first();
                    $systemAccount->numberIncrement($this->number);
                    $systemAccount->proofId = $traddingAccount->proofId;
                    $systemAccount->save();
                }

                // 加入待归集记录
                $wtrcCollect = new WtrcCollect();
                $wtrcCollect->address = $this->address;
                $wtrcCollect->number = $this->number;
                $wtrcCollect->save();

            });
        } catch (Throwable $exception) {
            Log::error($exception);
        }
    }
}
